#include <iostream>
#include <fstream>
#include <ostream>
#include <stdlib.h>
#include <string.h>
using namespace std;

int main()
{
    ifstream fin("scada.txt");
    if(!fin)
      {cout<<"file can not open"<<endl;}

    string a[97][88];
    int i,j;
    string s1,s2,s3;
    s1="CCBP";
    s2="PNP";
    s3="G3P";
    int s1_sub,s2_sub,s3_sub;
    for(i=0;i<97;i++)
      {
        for(j=0;j<87;j++)
           {getline(fin,a[i][j],',');}
       getline(fin,a[i][j],'\n');
      }

    for(j=0;j<88;j++)
    {if(a[0][j]==s1)
       s1_sub=j;
    if(a[0][j]==s2)
       s2_sub=j;
    if(a[0][j]==s3)
       s3_sub=j;}

    double sum_ccbp,sum_pnp,sum_g3p;
    sum_ccbp=0;
    sum_pnp=0;
    sum_g3p=0;
    for(i=1;i<97;i++)
    {
       sum_ccbp+=atof(a[i][s1_sub].c_str());
       sum_pnp+=atof(a[i][s2_sub].c_str());
       sum_g3p+=atof(a[i][s3_sub].c_str());
    }
    double average_dccnp=sum_ccbp/96;
    double average_dpnp=sum_pnp/96;
    double average_dg3p=sum_g3p/96;
    float  average_fccnp=float(sum_ccbp/96);
    float  average_fpnp=float(sum_pnp/96);
    float  average_fg3p=float(sum_g3p/96);

    ofstream outfile("scada1.txt");
    for(i=0;i<97;i++)
    {
       outfile<<a[i][0]<<'\t'<<a[i][1]<<'\t'<<a[i][s1_sub]<<'\t'<<a[i][s2_sub]<<'\t'<<a[i][s3_sub]<<endl;
    }
      printf("AVERAGE_FCCBP=%.16f\tAVERAGE_BCCBP=%.16f\n",average_fccnp,average_dccnp);
      printf("AVERAGE_FPNP=%.16f\tAVERAGE_BPNP=%.16f\n",average_fpnp,average_dpnp);
      printf("AVERAGE_FG3P=%.16f\tAVERAGE_BG3P=%20.16f\n",average_fg3p,average_dg3p);

    return 0;

}

